package com.healthcare;

import java.io.IOException;
import java.io.PrintWriter;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import webservices.HealthCareServerProxy;

import com.google.gson.Gson;

/**
 * Servlet implementation class UserRegistration
 */
public class UserRegistration extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public UserRegistration() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String address = null;

		if (request.getParameter("register") != null) {
			address = "/Registered.jsp";
		} else if (request.getParameter("listSites") != null) {
			address = "/Register.jsp";
		}

		RequestDispatcher dispatcher = request.getRequestDispatcher(address);
		dispatcher.forward(request, response);

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String inputfname = request.getParameter("fname");
		String inputLastName = request.getParameter("lname");
		String inputEmail = request.getParameter("email");
		String inputAge = request.getParameter("age");
		String inputGender = request.getParameter("sex");
		String inputPassword = request.getParameter("password1");

		User user = new User(inputfname, inputLastName, inputEmail, inputAge,
				inputGender, inputPassword);

		Connection con = null;
		Statement stmt = null;
		ResultSet rs = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			con = DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/aimsfirstconsultdb", "root",
					"");
			stmt = con.createStatement();

			String insertUserSql = "INSERT INTO users (first_name, last_name, email, age, gender, password) VALUES"
					+ "(\""
					+ (user.firstName)
					+ "\", \""
					+ (user.lastName)
					+ "\", \""
					+ (user.email)
					+ "\", \""
					+ (user.age)
					+ "\", \""
					+ (user.gender)
					+ "\", \""

					+ convertToMD5(user.password) + "\")";

			stmt.executeUpdate(insertUserSql);

			RequestDispatcher dispatcher = request
					.getRequestDispatcher("/Landing.jsp");
			dispatcher.forward(request, response);

		} catch (SQLException e) {
			throw new ServletException("Servlet Could not display records.", e);
		} catch (ClassNotFoundException e) {
			throw new ServletException("JDBC Driver not found.", e);

		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		
		
			try {
				if (rs != null) {
					rs.close();
					rs = null;
				}
				if (stmt != null) {
					stmt.close();
					stmt = null;
				}
				if (con != null) {
					con.close();
					con = null;
				}
			} catch (SQLException e) {
			}
		}
	



    private String convertToMD5(String input) throws NoSuchAlgorithmException {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(input.getBytes());
            byte byteData[] = md.digest();
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < byteData.length; i++)
                    sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16)
                                    .substring(1));
            return sb.toString();
    }

	class User {
		private String firstName;
		private String lastName;
		private String email;
		private String age;
		private String gender;
		private String password;

		public User(String firstName, String lastName, String email,
				String age, String gender, String password) {
			super();
			this.firstName = firstName;
			this.lastName = lastName;
			this.email = email;
			this.age = age;
			this.gender = gender;
			this.password = password;
		}

	}
}